﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FileManager_Client.BusinessLogic.FileManagerUserService;
using FileManager_Client.Utility;

namespace FileManager_Client.PresentationLayer.NewUi
{
    public partial class FileCopyToForm : BaseForm
    {
        #region Form Constructor
        private CompanyResultCollection companyCollection;
        private FolderResultCollection folderCollection;
        private UserLoginResult userSession;
        private FolderRoleResultCollection folderRoleCollection;
        public FolderFileDoEnum _folderFileDoEnum;
        public List<string> CopyToFolderSid { get; set; }
        public string CutFolderSid { get; set; }
        public string _folderOrfileSid { get; set; }
        public FileCopyToForm(FolderFileDoEnum folderFileDoEnum, string folderOrfileSid)
        {
            InitializeComponent();
            InitTreeView();
            _folderFileDoEnum = folderFileDoEnum;
            CopyToFolderSid = new List<string>();
            _folderOrfileSid = folderOrfileSid;
            if (folderFileDoEnum == FolderFileDoEnum.FolderCut || folderFileDoEnum == FolderFileDoEnum.FileCut)
                Text += " (剪切)";
            else
                Text += " (复制)";
        }
        private void InitTreeView()
        {
            companyCollection =
    CacheGlobal.GetCache(GlobalString.System_Config_String.CompanyCollectionCache) as
    CompanyResultCollection ??
    new CompanyResultCollection { CompanyResultsk__BackingField = new CompanyResult[0] };
            folderCollection =
                CacheGlobal.GetCache(GlobalString.System_Config_String.FolderCollectionCache) as
                FolderResultCollection ??
                new FolderResultCollection { FolderCollectionk__BackingField = new FolderResult[0] };
            folderRoleCollection =
                CacheGlobal.GetCache(GlobalString.System_Config_String.FolderRoleCollectionCache) as
                FolderRoleResultCollection ??
                new FolderRoleResultCollection { RoleResultsk__BackingField = new FolderRoleResult[0] };
            userSession =
                CacheGlobal.GetCache(GlobalString.System_Config_String.UserSession) as UserLoginResult
                ?? new UserLoginResult();
            tvFileView.Nodes.Clear();
            var rootNodeData =
               companyCollection.CompanyResultsk__BackingField.FirstOrDefault(x => x.Pidk__BackingField.Equals("0"));
            var rootNode = new TreeNode
            {
                Text = rootNodeData.ShortNamek__BackingField,
                ImageKey = GlobalString.System_Image_String.CompanyImageKey,
                Tag = rootNodeData,
                Name = rootNodeData.Idk__BackingField
            };

            rootNode.Expand();
            tvFileView.Nodes.Add(rootNode);
            var subNodeData =
                companyCollection.CompanyResultsk__BackingField.Where(
                    x => x.Pidk__BackingField.Equals(rootNodeData.Idk__BackingField)).ToList();
            foreach (var subNode in subNodeData.Select(companyResult => new TreeNode
            {
                Text = companyResult.ShortNamek__BackingField,
                ImageKey = GlobalString.System_Image_String.CompanyImageKey,
                Tag = companyResult,
                Name = companyResult.Idk__BackingField
            }))
            {
                rootNode.Nodes.Add(subNode);
            }
            //FIND ROOT SUBFOLDER
            var rootFolder =
                folderCollection.FolderCollectionk__BackingField.Where(x => x.ParentIdk__BackingField.Equals("0")).
                    ToList();
            foreach (var folderInfo in rootFolder)
            {
                var currentNode = tvFileView.Nodes.Find(folderInfo.OwnerIdk__BackingField, true);
                if (currentNode.Length <= 0) continue;
                var currentRoot = currentNode[0];
                var subFolderNode = new TreeNode
                {
                    Text = folderInfo.FileNamek__BackingField,
                    ImageKey = GlobalString.System_Image_String.FolderImageKey,
                    Tag = new NodeArgs { FolderResult = folderInfo },
                    Name = folderInfo.FileSidk__BackingField
                };
                var currentSubFolderNodes = ReturnTreeNode(folderInfo.FileSidk__BackingField);
                foreach (var currentSubFolderNode in currentSubFolderNodes)
                    subFolderNode.Nodes.Add(currentSubFolderNode);
                currentRoot.Nodes.Add(subFolderNode);
            }
            foreach (var folderRole in folderRoleCollection.RoleResultsk__BackingField)
            {
                var node = tvFileView.Nodes.Find(folderRole.FileIdk__BackingField, true);
                if (!node.Any()) continue;
                var objArgs = node[0].Tag as NodeArgs;
                if (null == objArgs) continue;
                objArgs.FolderRoleResult = folderRole;
                node[0].Tag = objArgs;
            }
            tvFileView.Update();
        }
        #endregion

        #region Button Confirm Event
        private IEnumerable<TreeNode> ReturnTreeNode(string parentId)
        {
            var result = new List<TreeNode>();
            var subFolders =
                folderCollection.FolderCollectionk__BackingField.Where(x => x.ParentIdk__BackingField.Equals(parentId)).
                    ToList();
            foreach (var folderResult in subFolders)
            {
                var subFolderNode = new TreeNode
                {
                    Text = folderResult.FileNamek__BackingField,
                    ImageKey = GlobalString.System_Image_String.FolderImageKey,
                    Tag = new NodeArgs { FolderResult = folderResult },
                    Name = folderResult.FileSidk__BackingField
                };
                var subsubNodes = ReturnTreeNode(folderResult.FileSidk__BackingField);
                foreach (var subsubNode in subsubNodes)
                    subFolderNode.Nodes.Add(subsubNode);
                result.Add(subFolderNode);
            }
            return result;
        }

        private void Btn_Confirm_Click(object sender, EventArgs e)
        {
            GetNodeId();
            if (_folderFileDoEnum == FolderFileDoEnum.FileCut || _folderFileDoEnum == FolderFileDoEnum.FolderCut)
            {
                if (CopyToFolderSid.Count > 0)
                    CutFolderSid = CopyToFolderSid[0];
            }
            Close();
        }
        #endregion

        #region TreeView Folder Collection Event
        private void tvFileView_BeforeCheck(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Checked) return;
            var obj = e.Node.Tag as CompanyResult;
            if (null == obj)
            {
                var folderObj = e.Node.Tag as NodeArgs;
                if ((null == folderObj.FolderRoleResult || !folderObj.FolderRoleResult.IsCreatek__BackingField) && !folderObj.FolderResult.OwnerIdk__BackingField.Equals(userSession.CompanyIdk__BackingField))
                {
                    XtraMessageBox.Show("您没有权限在这个文件夹进行剪切和粘贴操作!", GlobalString.System_Config_String.MbxInfoStr,
               MessageBoxButtons.OK, MessageBoxIcon.Information);
                    e.Cancel = true;
                    return;
                }
                foreach (TreeNode node in e.Node.Nodes)
                {
                    if (!node.Name.Equals(_folderOrfileSid)) continue;
                    XtraMessageBox.Show("当前目录剪切和复制操作没有发生变化!", GlobalString.System_Config_String.MbxInfoStr,
                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    e.Cancel = true;
                    return;
                }
                Checked = false;
                ReSetNode();
                if (Checked && (_folderFileDoEnum == FolderFileDoEnum.FileCut || _folderFileDoEnum == FolderFileDoEnum.FolderCut))
                {
                    XtraMessageBox.Show("剪切操作只能选择一个文件夹!", GlobalString.System_Config_String.MbxInfoStr,
                                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                    e.Cancel = true;
                    return;
                }
                var childNode = e.Node.Nodes.Find(_folderOrfileSid, true);
                if (e.Node.Name.Equals(_folderOrfileSid) || (null != childNode && childNode.Count() > 0))
                {
                    XtraMessageBox.Show("不能在同目录或者子目录下进行操作!", GlobalString.System_Config_String.MbxInfoStr,
                                      MessageBoxButtons.OK, MessageBoxIcon.Information);
                    e.Cancel = true;
                    return;
                }
            }
            else
            {
                XtraMessageBox.Show("不能在部门目录进行操作!", GlobalString.System_Config_String.MbxInfoStr,
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }
        #endregion

        #region Private Property
        private bool Checked { get; set; }
        #endregion

        #region Private Method
        private void ReSetNode()
        {
            foreach (TreeNode treeNode in tvFileView.Nodes)
            {
                Checked = Checked | treeNode.Checked;
                ReSetSubNode(treeNode);
            }
        }
        private void ReSetSubNode(TreeNode node)
        {
            foreach (TreeNode treeNode in node.Nodes)
            {
                Checked = Checked | treeNode.Checked;
                ReSetSubNode(treeNode);
            }
        }
        private void GetNodeId()
        {
            foreach (TreeNode treeNode in tvFileView.Nodes)
            {
                if (treeNode.Checked)
                {
                    var obj = treeNode.Tag as NodeArgs;
                    if (null != obj)
                        CopyToFolderSid.Add(obj.FolderResult.FileSidk__BackingField);
                }
                GetNodeSubNodeId(treeNode);
            }
        }
        private void GetNodeSubNodeId(TreeNode node)
        {
            foreach (TreeNode treeNode in node.Nodes)
            {
                if (treeNode.Checked)
                {
                    var obj = treeNode.Tag as NodeArgs;
                    if (null != obj)
                        CopyToFolderSid.Add(obj.FolderResult.FileSidk__BackingField);
                }
                GetNodeSubNodeId(treeNode);
            }
        }
        #endregion
    }

    #region Public Enum Folder Do Enum
    public enum FolderFileDoEnum
    {
        FolderCopy,
        FolderCut,
        FileCopy,
        FileCut
    }
#endregion
}